package com.example.leetdemo.process;

import java.util.*;
import java.util.stream.Collectors;

public class Solution2402 {

    public int mostBooked(int n, int[][] meetings) {
        List<int[]> list = Arrays.stream(meetings).sorted(Comparator.comparingInt(o -> o[0])).collect(Collectors.toList());
        PriorityQueue<int[]> queue = new PriorityQueue<>((o1, o2) -> {
            if (o1[0] == o2[0]) {
                return o1[2] - o2[2];
            }
            return o1[0] - o2[0];
        });
        int[][] dp = new int[n][3];
        for (int i = 0; i < n; i++) {
            dp[i][2] = i;
            queue.add(dp[i]);
        }
        for (int i = 0; i < list.size(); i++) {
            PriorityQueue<int[]> queue2 = new PriorityQueue<>(Comparator.comparingInt(o -> o[2]));
            while (true && queue.size() > 0) {
                if (queue.peek()[0] < list.get(i)[0]) {
                    queue2.add(queue.poll());
                } else {
                    break;
                }
            }
            if (queue2.size() > 0) {
                queue2.peek()[0] = list.get(i)[1];
                queue2.peek()[1]++;
                queue.addAll(queue2);
            } else {
                queue.peek()[0] = queue.peek()[0] - list.get(i)[0] + list.get(i)[1];
                queue.peek()[1]++;
            }
        }
        int t = 0, count = 0;
        for (int i = 0; i < n; i++) {
            if (dp[i][1] > count) {
                t = i;
                count = dp[i][1];
            }
        }
        return t;
    }

    public static void main(String[] args) {
        Solution2402 solution2402 = new Solution2402();
//        System.err.println(solution2402.mostBooked(4,
//                new int[][]{{18,19},{3,12},{17,19},{2,13},{7,10}}));
//        System.err.println(solution2402.mostBooked(3,
//                new int[][]{{1,20},{2,10},{3,5},{4,9},{6,8}}));
//        Set<String> set = new HashSet<>();
//        set.contains()

        PriorityQueue<int[]> queue = new PriorityQueue<>((o1, o2) -> {
            if (o1[0] == o2[0]) {
                return o1[2] - o2[2];
            }
            return o1[0] - o2[0];
        });
        int n = 3;
        int[][] dp = new int[][] {{10,2,2}, {10,1,1}, {20,1,0}};
        for (int i = 0; i < n; i++) {
//            dp[i][2] = i;
            queue.add(dp[i]);
        }
        System.err.println(queue.size());
    }

    private void temp() {
//        int[] temp = queue.poll();
//        if (temp[0] <= list.get(i)[0]) {
//            temp[0] = list.get(i)[1];
//            temp[1]++;
//            queue.add(temp);
//        } else {
//            PriorityQueue<int[]> queue2 = new PriorityQueue<>(Comparator.comparingInt(o -> o[2]));
//            queue2.add(temp);
//            while (queue.size() > 0) {
//                int[] t1 = queue.poll();
//                if (t1[0] > list.get(i)[1]) {
//                    break;
//                } else {
//                    queue2.add(t1);
//                }
//            }
//            temp = queue2.peek();
//            temp[0] = list.get(i)[1] - list.get(i)[0] + temp[0];
//            temp[1]++;
//            queue.addAll(queue2);
//        }

    }
}
